package com.cloud.animationtestdemo.strage

import android.animation.PropertyValuesHolder
import android.animation.ValueAnimator
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator

/**
 * @author: njb
 * @date:   2025/1/1 23:48
 * @desc:   描述
 */
class InterpolatorAnimationStrategy :BaseAnimationStrategy{
    private val translationXProperty = PropertyValuesHolder.ofFloat("translationX", 0f)
    private val translationYProperty = PropertyValuesHolder.ofFloat("translationY", 0f)

    override fun getAlphaForProgress(progress: Float): Float {
        // 自定义透明度与进度关联，例如进度前期透明度变化快，后期慢
        return if (progress < 0.5f) progress * 2f else (1 - progress) * 2f
    }

    fun animateView(view: View, startX: Float, startY: Float, endX: Float, endY: Float, duration: Long) {
        translationXProperty.setFloatValues(startX, endX)
        translationYProperty.setFloatValues(startY, endY)

        val animator = ValueAnimator.ofPropertyValuesHolder(translationXProperty, translationYProperty)
        animator.duration = duration
        // 设置先快后慢的插值器（这里使用AccelerateDecelerateInterpolator作为示例）
        animator.interpolator = AccelerateDecelerateInterpolator()
        animator.addUpdateListener { animation ->
            val currentX = (animation as? ValueAnimator)?.getAnimatedValue(translationXProperty.propertyName) as? Float?: 0f
            val currentY = (animation as? ValueAnimator)?.getAnimatedValue(translationYProperty.propertyName) as? Float?: 0f
            view.translationX = currentX
            view.translationY = currentY
            val progress = animation.animatedFraction
            val alpha = getAlphaForProgress(progress)
            view.alpha = alpha
        }
        animator.start()
    }
}